package br.com.werp.suporte.Empresa;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 19/10/2004 13:50:31
 * Last Modify Date 17/10/2006 14:40:30
 */

public class SetorDetalheAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_update_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    if (com.egen.util.system.Security.isUserInRole(request, "SUP_ADM",false)) {
      HttpSession session = request.getSession(true);
      ActionForward actionForward = null;
      com.egen.util.jdbc.JdbcUtil j = null;
      try {
        j = new com.egen.util.jdbc.JdbcUtil();
        update_action(j, mapping, form, request, response);
        actionForward = mapping.findForward("voltar");
      } catch (Exception e) {
        ActionErrors errors = new ActionErrors();
        errors.add("ActionErrors.GLOBAL_ERROR",
            new ActionError("error.action.exception",
                (e)));
        request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
        session.setAttribute("exception",
            (e));
        actionForward = mapping.findForward("same");
      }
      finally { if (j != null) {
            j.close();
          }
              } return actionForward;
    } else {
      ActionErrors errors_role = new ActionErrors();
      errors_role.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.actiondenied",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors_role);
      return mapping.findForward("same");
    }
  }
  private void update_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    SetorDetalheActionForm f = (SetorDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Setor table =
      new br.com.werp.suporte.dbobj.table.Setor();
    Vector sets = new Vector(10, 2);
    table.setStor_id(
      com.egen.util.text.FormatNumber.parseInt(f.getStor_id()));
    table.setStor_descricao(f.getStor_descricao());
    if (f.getStor_descricao() != null) {
      sets.addElement("stor_descricao");
    } else {
      sets.addElement("");
    };
    String[] set = new String[sets.size()];
    for (int i = 0; i < sets.size(); i++) {
      set[i] = (String) sets.elementAt(i);
    }
    int updated = j.update(table, set);
    if (updated == 0) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsupdated",": " + updated));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    } else {
      Vector res_Setor = (Vector) session.getAttribute("res_Setor");
      if (res_Setor != null) {
        res_Setor.setElementAt(table, 0);
        br.com.werp.suporte.dbobj.table.Setor r =
          (br.com.werp.suporte.dbobj.table.Setor)
          res_Setor.elementAt(0);
        f.setStor_id(r.getStor_id() + "");
        f.setStor_descricao((r.getStor_descricao() != null) ?
            r.getStor_descricao() + "":"");
      }
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_delete_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      delete_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void delete_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    SetorDetalheActionForm f = (SetorDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Setor table =
      new br.com.werp.suporte.dbobj.table.Setor();
    table.setStor_id(
      com.egen.util.text.FormatNumber.parseInt(f.getStor_id()));
    table.setStor_descricao(f.getStor_descricao());
    j.delete(table);
    if (false) {
      Vector res_Setor = (Vector) session.getAttribute("res_Setor");
      if (res_Setor != null) {
        res_Setor.removeElementAt(0);
        br.com.werp.suporte.dbobj.table.Setor r =
          (br.com.werp.suporte.dbobj.table.Setor)
          res_Setor.elementAt(0);
        f.setStor_id(r.getStor_id() + "");
        f.setStor_descricao((r.getStor_descricao() != null) ?
            r.getStor_descricao() + "":"");
      }
    } else {
      f.reset(mapping, request);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_back_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      select_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    SetorDetalheActionForm f = (SetorDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Setor table =
      new br.com.werp.suporte.dbobj.table.Setor();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    Object[][] where = { {"Stor_id","=",
        com.egen.util.text.FormatNumber.toInt(f.getStor_id())}, {"Stor_descricao",
            "like", f.getStor_descricao()} };
    Vector res_Setor = j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Setor",res_Setor);
    if (res_Setor != null && res_Setor.size() > 0) {
      br.com.werp.suporte.dbobj.table.Setor r =
        (br.com.werp.suporte.dbobj.table.Setor)
        res_Setor.elementAt(0);
      f.setStor_id(r.getStor_id() + "");
      f.setStor_descricao((r.getStor_descricao() != null) ?
          r.getStor_descricao() + "":"");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute(mapping.getName(), form);
  }

}
